package com.intellagric.service.traceability.Impl.trace_record;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.RecordMachiningDao;
import com.intellagric.dao.traceability.RecordMonitorDao;
import com.intellagric.mapper.TraceRecordMonitorMapper;
import com.intellagric.pojo.TraceRecordMonitor;
import com.intellagric.pojo.TraceRecordMonitorExample;
import com.intellagric.service.traceability.trace_record.MonitorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/2/25
 * @DESC： 监控数据服务接口
 */
@Service
public class MonitorRecordServiceImpl implements MonitorRecordService {

    @Autowired
    private TraceRecordMonitorMapper recordMonitorMapper;

    @Autowired
    private RecordMonitorDao recordMonitorDao;

    /**
     * 添加监控设备
     * @param recordMonitor
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addMonitorRecord(TraceRecordMonitor recordMonitor) {
        if (recordMonitorMapper.insertSelective(recordMonitor) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除监控设备
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteIMonitorRecord(String id) {
        if (recordMonitorMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改监控设备
     * @param recordMonitor
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editMonitorRecord(TraceRecordMonitor recordMonitor) {
        if (recordMonitorMapper.updateByPrimaryKeySelective(recordMonitor) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 根据id查询监控设备
     * @param id
     * @return ResponseMessage
     */
    @Override
    public TraceRecordMonitor getMonitorRecordById(String id) {
        return recordMonitorMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询监控设备列表
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getMonitorRecordList() {
        TraceRecordMonitorExample traceRecordMonitorExample = new TraceRecordMonitorExample();
        traceRecordMonitorExample.createCriteria().andIdIsNotNull();
        List<TraceRecordMonitor> recordMonitorList = recordMonitorMapper.selectByExample(traceRecordMonitorExample);
        return LayuiDataGridResult.success().add(recordMonitorList,recordMonitorList.size());
    }

    /**
     * 分页查询监控设备列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMonitorRecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordMonitorExample traceRecordMonitorExample = new TraceRecordMonitorExample();
        traceRecordMonitorExample.createCriteria().andIdIsNotNull();
        List<TraceRecordMonitor> recordMonitorList = recordMonitorMapper.selectByExample(traceRecordMonitorExample);
        PageInfo<TraceRecordMonitor> pageInfo = new PageInfo<>(recordMonitorList);
        return LayuiDataGridResult.success().add(recordMonitorList,pageInfo.getTotal());
    }

    /**
     * 分页查询监控设备列表
     * @param page
     * @param rows
     * @param recordMonitor
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMonitorRecordPageByVo(int page, int rows, TraceRecordMonitor recordMonitor) {
        PageHelper.startPage(page,rows);
        List<TraceRecordMonitor> recordMonitorList = recordMonitorDao.selectTraceRecordMonitorPageByVo(recordMonitor);
        PageInfo<TraceRecordMonitor> pageInfo = new PageInfo<>(recordMonitorList);
        return LayuiDataGridResult.success().add(recordMonitorList,pageInfo.getTotal());
    }
}
